﻿function uploadingFile(file, sucressCallBack, failCallBack, cancelCallBack) {
    var fd = new FormData();
    fd.append("theFile", file);
    var ajaxRequest = $.ajax({
        url: "/UploadFile/UploadingFile",
        type: 'POST',
        data: fd,
        processData: false,//用来回避jquery对formdata的默认序列化，XMLHttpRequest会对其进行正确处理
        contentType: false,//设为false才会获得正确的conten-Type
        async: true
    }).then(function (data) {
        if (data.IsSurcess) {
            sucressCallBack(data)
        }
        else if (data.IsFail) {
            failCallBack(data);
        }
        else {
            cancelCallBack();
        }
        //$.globalEval($(data).text());
    });
    return ajaxRequest;
}


function checkLoadFile(file) {
    if (!file) {
        alert('请选择上传文件');
        return false;
    }
    if (!(file.type == 'image/jpeg' || file.type == 'image/png' || file.type == 'image/bmp')) {
        alert('文件格式必须为.jpg、png、bmp三种格式之一');
        return false;
    }
    if (file.size > 1048576 * 2) {
        alert('文件大小不能超过2M');
        return false;
    }
    return true;
}

function _uploadedSurcessHandel(row, data)
{
    row.before(CreateUploadedRow(data.FileName, 'FileNames'));
    row.remove();
}

function _uploadedFailHandel(data)
{
    alert(data.error);
}

function _uploadedCancelHandel(row)
{
    row.before(CreateUploadRow());
    row.remove();
}

function CreateUploadRow(uploadedSurcessHandel,uploadedFailHandel,uploadedCancelHandel) {
    var row = $('<tr></tr>');
    var td1 = $('<td class="left_td"></td>').appendTo(row);
    td1.addClass("left_td");
    var td2 = $('<td class="right_td"></td>').appendTo(row);
    var loadingBlock = $('<a class="wait" style="display:none">正在上传...</a>').appendTo(td1);
    var cancelBtn = $('<a class="cancelUploadBtn" style="display:none;">取消上传</a>').appendTo(td1);
    var uploadBtn = $('<a class="uploadBtn">上传</a>').appendTo(td2);
    var fileContainer = $('<div class="fileContainer"></div>').appendTo(td2);
    var uploadControl = $('<input type="file" accept=".png,.jpg,.bmp"/>').appendTo(fileContainer);
    uploadBtn.click(function () {
        uploadControl.click();
    });

    var ajaxRequest = null;

    uploadControl.change(function () {
        if (!uploadControl[0].files[0])
            return;
        if (checkLoadFile(uploadControl[0].files[0])) {
            uploadControl.attr('disabled', 'disabled');
            loadingBlock.show();
            cancelBtn.show();
            ajaxRequest = uploadingFile(uploadControl[0].files[0], function (data) {
                uploadedSurcessHandel ? uploadedSurcessHandel(row, data) : _uploadedSurcessHandel(row, data);
            },
            function (data) {
                uploadedFailHandel ? uploadedFailHandel(row, data) : _uploadedFailHandel(data);
            },
            function () {

            });
        }
    });

    cancelBtn.click(function () {
        if (ajaxRequest) {
            ajaxRequest.abort('cancel');
            uploadedCancelHandel ? uploadedCancelHandel(row, data) : _uploadedCancelHandel(row);
        }
    })

    return row;
}

    function CreateUploadedRow(fileName,hideElementName,clickHandel) {
        var row = $('<tr></tr>');
        var td1 = $('<td></td>').appendTo(row);
        td1.addClass("left_td");
        var td2 = $('<td class="right_td"></td>').appendTo(row);
        var loadingBlock = $('<a></a>').appendTo(td1);
        loadingBlock.attr('href', "./../Attachments/" + fileName);
        loadingBlock.attr('target', "_blank");
        loadingBlock.html(fileName)
        var hiden = $('<input type="hidden">').appendTo(td1);
        hiden.attr('id', hideElementName);
        hiden.attr('name', hideElementName);
        hiden.val(fileName);
        var deleteBtn = $('<a class="deleteBtn">删除</a>').appendTo(td2);


        deleteBtn.click(function () {
            if (clickHandel)
            {
                clickHandel(row);
            }
            else
            {
                row.remove();
            }
            });

        return row;
    }